﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Threading;


namespace iOSRemoteConnector
{
    public class iOSSocket
    {

        static int nextPortNumber = 60000;


        private String uuid = null; //identifies the iPhone paired to this socket
        private TcpListener server = null;
        private StreamWriter logger = null;
        private IPAddress ip = null;

        public Queue<MethodCall> methodQueue = null; 

        public int portNumber { get; set; }



        public iOSSocket(String identification)
        {

            uuid = identification; 


            //Setup the IPAddress 
            ip = IPAddress.Parse("5.175.235.79");

            //Intialize the methodQueue
            methodQueue = new Queue<MethodCall>(); 




            //Setup the logger for that particular connection
            String fileName = "log" + uuid + ".txt"; 

            logger = new StreamWriter(Path.Combine(@"C:\Temp\Logs", fileName));
  
            portNumber = nextPortNumber; 
            nextPortNumber++; 


        
        }
        public void RunServer()
        {


            //Using the IP Address and Port Number We Start the Server
            try
            {

                server = new TcpListener(ip,portNumber);
                logger.WriteLine(server.LocalEndpoint.ToString());
                logger.Flush();

                server.Start();
            }

            catch (SocketException e)
            {

                logger.WriteLine(e);
                logger.Flush();

            }


            

            
            logger.WriteLine("Waiting on The Connection");
            logger.Flush();
            
            
            //This blocking call will pause until connection is made by the IOS device
            Socket client = server.AcceptSocket();


                
            if (client.Connected)
            {
                    
                logger.WriteLine("Connected");
                logger.Flush();


                NetworkStream ns = new NetworkStream(client);
                StreamWriter sw = new StreamWriter(ns);
                StreamReader sr = new StreamReader(ns);



  
                while ( true)
                {

                    if (methodQueue.Count > 0) //Runs only if there are calls.
                    {
                        MethodCall call = methodQueue.Dequeue(); 

                        sw.WriteLine(call.ToString());
                        sw.Flush();
                        logger.WriteLine(call);
                        logger.Flush();

                        //Devices.ProcessingCall(call); 



                    }

 
                }

                sw.Close();
                sr.Close();
                ns.Close();



            }
            logger.Close();  
            client.Close();

        }

        //TODO: Fill in this method
        public void EndServer() 
        {
            //Thread thisThread = Thread.CurrentThread;
            //thisThread.Abort(); 

        
        }



    }

}
